<?php
namespace Api\Model;

use Think\Model;

class OrderModel extends Model
{

    /**
     * @param string $lng //经度
     * @param string $lat //纬度
     * @param string $modeOrder //订单类型（1即时订单2预约订单）
     * @param string $modeCharging //计费方式(1顺风2标准3专修)
     * @return mixed
     */
    public function getMasterList($user_id,$lng, $lat, $modeOrder, $modeCharging, $category_id, $pageSize ,$pageIndex , $type)
    {
        $data=array();
        $limit = ($pageIndex-1)*$pageSize;

        if($type==1){
            $order = "juli asc";
        }elseif($type==2){
            $order = "inf.grade desc";
        }elseif($type==3){
            $order = "inf.complete desc";
        }
        $where = "u.role = 2 and m.mode_cate=1 and FIND_IN_SET($modeOrder,m.mode_order) and u.is_real = 1 and FIND_IN_SET($modeCharging,m.mode_charging) and $category_id in(c.category_id) and m.user_id != $user_id";

        $juli = "ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$lat}*PI()/180-lat*PI()/180)/2),2)+COS({$lat}*PI()/180)*COS(lat*PI()/180)*POW(SIN(({$lng}*PI()/180-lng*PI()/180)/2),2)))*1000)";

        $userList = M('UserMode m')
            ->DISTINCT(TRUE)
            ->field("{$juli} as juli,user_face as master_face,phone as master_phone,u.user_id,inf.id_name as master_name,inf.grade,inf.complete")
            ->join('__USER__ u ON u.user_id = m.user_id')
            ->join('zsf_user_real_info inf on inf.user_id = m.user_id')
            ->join('zsf_user_repair_conf c on c.user_id = m.user_id')
            ->where($where)
            ->order("{$order}")
            ->page("$limit,$pageSize")
            ->select(false);

        //30公里以内
        $sql = "select * from  ($userList) as a where a.juli<=30000";
        $sqlCount = "select count(*) as count from  ($userList) as a where a.juli<=30000";
        $count = M()->query($sqlCount);
        if($count){
            $pages = (int)ceil($count[0]['count']/$pageSize);
        }else{
            $pages = 0;
        }
        $info = M()->query($sql);
        if(!$info) {
            $info = array();
        }
        $data['data'] = $info;
        $data['pages'] = $pages;

        return $data;
    }


}
